package com.ruoyi.web.controller.statistics;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.RetResponse;
import com.ruoyi.common.enums.RetResult;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.JsapiTicketUtil;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.config.WxConfig;
import org.apache.avro.data.Json;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author ONESTAR
 * @Date: 2021/7/16 16:31
 * @Version 1.0
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {

    private String prefix = "system/statistics";

    @Autowired
    private IStatisticsService statisticsService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 加载部门列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = statisticsService.selectDeptTree(new SysDept());
        return ztrees;
    }

    /**
     * 注册界面 -> 初始化项目机构树
     *
     * @return
     */
    @GetMapping()
    public String record() {
        return prefix + "/index";
    }

    /**
     * 运输车主界面 -> 核查用户
     *
     * @return
     */
    @RequestMapping("/queryDayData")
    @ResponseBody
    public Map<Object, Object> queryDayData(@RequestBody Map<String, String> map) {
        logger.info("queryDayData() queryDayData:{}", map);

        // 日期的准备
        formatDate(map);

        // 获取当前的用户信息
        SysUser user = ShiroUtils.getSysUser();
        if(user.getLoginName().equals("admin") || user.getDeptId() == 101){
            // admin用户-展示折线图堆叠
            Map<Object, Object> resMap = new HashMap<>();
            List<Ztree> ztrees = statisticsService.selectPrograms(new SysDept());
            List<String> nameList = new ArrayList<>();
            List<List<String>> dataList = new ArrayList<>();
            List<String> dateList = new ArrayList<>();
            if(StringUtils.isNotEmpty(ztrees)){
                for(Ztree tree : ztrees){
                    map.put("programName", tree.getName());
                    List<String> numList = new ArrayList<>();
                    List<Map<String, String>> maps = statisticsService.queryDayData(map);
                    if(StringUtils.isNotEmpty(maps)){
                        for(Map<String, String> mapObj : maps){
                            String num = String.valueOf(mapObj.get("num"));
                            String date = mapObj.get("date");
                            numList.add(num);
                            dateList.add(date);
                        }
                    }
                    nameList.add(tree.getName());
                    dataList.add(numList);
                }
            }

            List<Map<String, String>> ringMaps = statisticsService.queryProgramRingData(map);

            // 去重
            List<String> dateLists = dateList.stream().distinct().collect(Collectors.toList());
            resMap.put("nameList", nameList);
            resMap.put("dataList", dataList);
            resMap.put("dateList", dateLists);
            resMap.put("ringMaps", ringMaps);
            resMap.put("flag", "admin");
            return resMap;

        }else{
            // 非admin用户-根据项目id取项目名称
            String deptName = deptService.selectDeptById(user.getDeptId()).getDeptName();
            map.put("programName", deptName);
            Map<Object, Object> resMap = new HashMap<>();
            List<String> numList = new ArrayList<>();
            List<String> dateList = new ArrayList<>();

            List<Map<String, String>> maps = statisticsService.queryDayData(map);
            if(StringUtils.isNotEmpty(maps)){
                for(Map<String, String> mapObj : maps){

                    String num = String.valueOf(mapObj.get("num"));
                    String date = mapObj.get("date");
                    numList.add(num);
                    dateList.add(date);
                }
            }

            List<Map<String, String>> ringMaps = statisticsService.queryRingData(map);

            resMap.put("numList", numList);
            resMap.put("dateList", dateList);
            resMap.put("ringMaps", ringMaps);
            resMap.put("flag", "user");
            return resMap;
        }
    }

    /**
     * 运输车主界面 -> 核查用户
     *
     * @return
     */
    @RequestMapping("/queryRingData")
    @ResponseBody
    public Map<Object, Object> queryRingData(@RequestBody Map<String, String> map) {

        // 日期的准备
        formatDate(map);

        Map<Object, Object> resMap = new HashMap<>();
        List<String> numList = new ArrayList<>();
        List<String> supplierNameList = new ArrayList<>();
        List<Map<String, String>> maps = statisticsService.queryRingData(map);
        if(StringUtils.isNotEmpty(maps)){
            for(Map<String, String> mapObj : maps){
                String num = String.valueOf(mapObj.get("num"));
                String date = mapObj.get("supplierName");
                numList.add(num);
                supplierNameList.add(date);
            }
        }
        resMap.put("numList", numList);
        resMap.put("supplierNameList", supplierNameList);
        return resMap;
    }

    private void formatDate(@RequestBody Map<String, String> map) {
        Date CurrentTime = new Date();
        String end = new SimpleDateFormat("yyyy-MM-dd").format(CurrentTime);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -6);
        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd");
        String start = sp.format(cal.getTime());

        String startTime = map.get("startTime") == "" ? start : map.get("startTime");
        String endTime = map.get("endTime") == "" ? end : map.get("endTime");
        map.put("startTime", startTime);
        map.put("endTime", endTime);
    }


}
